import BaseController from '../../../core/baseController';
import { getCurrentStimp } from '../../../utils/index';

export default class StoreMiniController extends BaseController {
  constructor(ctx) {
    super(ctx)
    this.service = ctx.service.platform.store.mini,
    this.modelPrefix = 'store.mini'
  }

  // 通用列表
  async myList() {
    const { page, pageSize, ...where } = this.ctx.validateReq(
      `${this.modelPrefix}_my_list`,
    ).query;

    const result = await this.ctx.service.platform.store.appUserPage.index(page, pageSize, where);

    if (result) {
      this.ctx.success({
        data: result,
      });
    }
  }


  // 通用列表
  async list() {
    const { page, pageSize, ...where } = this.ctx.validateReq(
      `${this.modelPrefix}_list`,
    ).query;

    const result = await this.service.index(page, pageSize, where);

    if (result) {
      this.ctx.success({
        data: result,
      });
    }
  }


  // 创建
  async create() {
    const { body } = this.ctx.validateReq(`${this.modelPrefix}_create`);
    const stemp = getCurrentStimp();
    Object.assign(body, {
      create_at: stemp,
      update_at: stemp,
    });
    const result = await this.service.create(body);

    if (result) {
      this.ctx.success({
        data: {
          id: result.id,
        },
        status: 201,
      });
    }
  }


  // 编辑
  async update() {
    const { body, params: { id } } = this.ctx.validateReq(`${this.modelPrefix}_update`);

    const stemp = getCurrentStimp();
    Object.assign(body, {
      update_at: stemp,
    });

    const result = await this.service.update(id, body);

    if (result) {
      this.ctx.success({
        data: {
          id: result.id,
        },
      });
    }
  }

  // 软删除
  async destroy() {

    const { params: { id } } = this.ctx.validateReq(`${this.modelPrefix}_destroy`);
    const result = await this.service.delete(id);
    if (result) {
      this.ctx.success({
        data: {
          id: result.id,
        },
      });
    }
  }

  // 详情
  async detail() {
    const { params: { id } } = this.ctx.validateReq(`${this.modelPrefix}_detail`);
    const result = await this.service.detail(id);
    if (result) {
      this.ctx.success({
        data: result,
      });
    }
  }

}
